package com.vnp.cooking.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Typeface;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.vnp.cooking.ChitietCongthucActivity;
import com.vnp.cooking.CommentActivity;
import com.vnp.cooking.R;
import com.vnp.cooking.XemInfoUserActivity;
import com.vnp.cooking.XemThongTinUserActivity;
import com.vnp.cooking.UIcontrols.MyCircleImageView;
import com.vnp.cooking.UIcontrols.MyClickAbleImageView;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.utils.Utils;

public class NhabepListAdpater extends ArrayAdapter<ObjCookpad> implements OnClickListener{
	
	Context context;
	ArrayList<ObjCookpad> objCookpads;
	
	private int lastPosition = -1;
	
	// thiet lap image loader
		DisplayImageOptions options  = new DisplayImageOptions.Builder()
		  								.cacheInMemory(false)
		  								.cacheOnDisc(true)
		  								.resetViewBeforeLoading(true)
		  								.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		  								.bitmapConfig(Config.RGB_565)
		  								.build();
	
	public NhabepListAdpater(Context context, ArrayList<ObjCookpad> objCookpads) {
		super(context, R.layout.nhabep_list_item, objCookpads);
		this.context = context;
		this.objCookpads = objCookpads;
	}

	@Override
	public int getCount() {
		return objCookpads.size();
	}
	
	

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		ViewHolder holder = null;
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.nhabep_list_item, parent, false);
			holder = new ViewHolder();
			
			holder.imgMonAn = (MyClickAbleImageView) v.findViewById(R.id.img_monan);
			holder.txtTenMonAn = (TextView) v.findViewById(R.id.txt_ten_mon_an);
			holder.txtTenNguoidang = (TextView) v.findViewById(R.id.txt_ten_nguoi_dang);
			holder.txtViewer = (TextView) v.findViewById(R.id.txt_viewer);
			holder.txtLiker = (TextView) v.findViewById(R.id.txt_liked_count);
			holder.txtThoigiannau = (TextView) v.findViewById(R.id.txt_thoi_gian);
			holder.imgEdit = (ImageButton) v.findViewById(R.id.btn_edit_congthuc_dadang);
			
			//font
			holder.txtTenMonAn.setTypeface(Utils.getTypefaceBold(context), Typeface.BOLD);
			holder.txtTenNguoidang.setTypeface(Utils.getTypefaceRegular(context),Typeface.BOLD);
			holder.txtViewer.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtLiker.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtThoigiannau.setTypeface(Utils.getTypefaceRegular(context));
			
			v.setTag(holder);
			
		} else {
			holder = (ViewHolder) v.getTag();
		}
		
		// set gia tri
		ImageLoader.getInstance().displayImage(objCookpads.get(position).getUrlImageCookpad(), holder.imgMonAn, options);
		// neu ten dai qua thi cong them ...
		
		if (objCookpads.get(position).getNameCookpad().length() > 20) {
			holder.txtTenMonAn.setText(objCookpads.get(position).getNameCookpad().substring(0, 20)+ "...");
		} else {
			holder.txtTenMonAn.setText(objCookpads.get(position).getNameCookpad());
		}
		holder.txtTenNguoidang.setText(objCookpads.get(position).getNamePersonCookpad());
		holder.txtViewer.setText(""+objCookpads.get(position).getViewedCookpad());
		holder.txtLiker.setText(""+objCookpads.get(position).getLikeCookpad());
		holder.txtThoigiannau.setText(objCookpads.get(position).getThoigianNauan());
		holder.imgMonAn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent c = new Intent(context, ChitietCongthucActivity.class);
				c.putExtra("objCookpad", objCookpads.get(position));
				v.getContext().startActivity(c);
			}
		});
		
		// sang trang ca nhan nguoi khac
		holder.txtTenNguoidang.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent c = new Intent(context, XemThongTinUserActivity.class);
				c.putExtra("objCookpad", objCookpads.get(position));
				v.getContext().startActivity(c);
			}
		});
		
		// Animation
		Animation animation = AnimationUtils.loadAnimation(getContext(), (position > lastPosition) ? R.anim.listview_up_from_bottom : R.anim.listview_down_from_bottom);
		v.startAnimation(animation);
		lastPosition = position;

		
		return v;
	}
	
	
	static class ViewHolder {
		ImageView imgMonAn;
		TextView txtTenMonAn;
		TextView txtTenNguoidang;
		TextView txtViewer;
		TextView txtLiker;
		TextView txtThoigiannau;
		ImageView imgEdit;
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_monan:
			break;

		default:
			break;
		}
	}
	
	
	
	// sort list object moi nhat
	public void sortByNewAsc() {
		Comparator<ObjCookpad> comparator = new Comparator<ObjCookpad>() {

			@Override
			public int compare(ObjCookpad obj1, ObjCookpad obj2) {
				
				return ((Integer) obj2.getViewedCookpad()).compareTo((Integer) obj1.getViewedCookpad());
			}
		};
		Collections.sort(objCookpads, comparator);
		notifyDataSetChanged();
	}
	
	// sort list xemnhieur
	public void sortByViewAsc() {
		Comparator<ObjCookpad> comparator = new Comparator<ObjCookpad>() {

			@Override
			public int compare(ObjCookpad obj1, ObjCookpad obj2) {
				
				return ((Integer) obj2.getViewedCookpad()).compareTo((Integer) obj1.getViewedCookpad());
			}
		};
		Collections.sort(objCookpads, comparator);
		notifyDataSetChanged();
	}
	
	// sap xep theo yeu thich
	public void sortByLikeAsc() {
		Comparator<ObjCookpad> comparator = new Comparator<ObjCookpad>() {

			@Override
			public int compare(ObjCookpad obj1, ObjCookpad obj2) {
				
				return ((Integer) obj2.getLikeCookpad()).compareTo((Integer) obj1.getLikeCookpad());
			}
		};
		Collections.sort(objCookpads, comparator);
		notifyDataSetChanged();
	}
	
	/*// sap xep theo danh gia 
	public void sortByRateAsc() {
		Comparator<ObjCookpad> comparator = new Comparator<ObjCookpad>() {

			@Override
			public int compare(ObjCookpad obj1, ObjCookpad obj2) {
				
				return ((Integer) obj2.getRatingCookpad()).compareTo((Integer) obj1.getRatingCookpad());
			}
		};
		Collections.sort(objCookpads, comparator);
		notifyDataSetChanged();
	}*/
}
